package com.starhub.system.database.runner;

import com.starhub.system.database.config.DatabaseUpgradeConfig;
import com.starhub.system.database.service.DatabaseUpgradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 数据库升级启动执行器
 * 在系统启动完成后自动执行数据库升级
 */
@Slf4j
@Component
@Order(1)
public class DatabaseUpgradeRunner implements ApplicationRunner {
    
    @Autowired
    private DatabaseUpgradeConfig config;
    
    @Autowired
    private DatabaseUpgradeService databaseUpgradeService;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("系统启动完成，开始执行数据库升级...");
        
        try {
            // 延迟执行，确保数据库连接池完全初始化
            long delay = config.getStartupDelay();
            log.info("等待 {} 毫秒后开始执行数据库升级...", delay);
            Thread.sleep(delay);
            
            databaseUpgradeService.executeUpgrade();
            
        } catch (Exception e) {
            log.error("数据库升级执行异常: {}", e.getMessage(), e);
        }
    }
} 